package com.myapplication.ui.homefragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.myapplication.R;
import com.myapplication.ui.homefragment.activities.Activity_Item1;
import com.myapplication.ui.homefragment.activities.Activity_Item2;
import com.myapplication.ui.homefragment.activities.Activity_Notification;
import com.myapplication.ui.homefragment.activities.Activity_Search;
import com.myapplication.ui.homefragment.lifefragment.Fragment_Life;
import com.myapplication.ui.homefragment.shoefragment.Fragment_Shoe;
import com.myapplication.ui.homefragment.beautifulfragment.Fragment_Beautiful;
import com.myapplication.ui.homefragment.shopfragment.Fragment_Shop;
import com.myapplication.ui.myactivity.LoginActivity;
import com.myapplication.ui.myactivity.MainInfoActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobUser;

/**
 * Created by zitaojiang on 2016/10/6.
 */
public class Fragment_Home extends Fragment {
    @Bind(R.id.tablayout)
    TabLayout tablayout;
    @Bind(R.id.vp)
    ViewPager vp;
    @Bind(R.id.toolbar)
    Toolbar toolbar;

    private List<Fragment> fragmentList;
    private int[] titles = {R.string.shop, R.string.shoe, R.string.life, R.string.beautiful};
    private MyHomePagerAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, null);
        ButterKnife.bind(this, view);
        initTitles();
        initFragments();
        //将tablayout与ViewPager的页面改变监听事件绑定，切换页面时，tab按钮切换到对应的选中状态。
        vp.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tablayout));
        //tablayout绑定其自己的Tab导航栏，监听选中某个按钮时，切换到对应的页面。和
        tablayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                vp.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        initToolBar();
        return view;
    }

    private void initToolBar() {

        toolbar.inflateMenu(R.menu.tool_menu);

        //设置Navigation的点击事件
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取当前的登陆状态
                BmobUser user = BmobUser.getCurrentUser();
                if (user!=null){
                    //登陆，跳转到个人登陆页面
                    Intent intent_Main=new Intent(getActivity(), MainInfoActivity.class);
                    startActivity(intent_Main);

                }else {
                    //没有登陆，跳转到登陆页面
                    Intent intent_Login=new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent_Login);
                }

            }
        });
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                int MenuItenId=item.getItemId();
                switch (MenuItenId){
                    case R.id.action_search:
                        Intent intent_search=new Intent(getActivity(), Activity_Search.class);
                        startActivity(intent_search);
                        break;
                    case R.id.action_notification:
                        Intent intent_notification=new Intent(getActivity(), Activity_Notification.class);
                        startActivity(intent_notification);
                        break;
                    case R.id.action_item1:
                        Intent intent_item1=new Intent(getActivity(), Activity_Item1.class);
                        startActivity(intent_item1);
                        break;
                    case R.id.action_item2:
                        Intent intent_item2=new Intent(getActivity(), Activity_Item2.class);
                        startActivity(intent_item2);
                        break;
                }
                return false;
            }
        });
    }

    private void initFragments() {
        fragmentList = new ArrayList<>();

        fragmentList.add(new Fragment_Shop());
        fragmentList.add(new Fragment_Shoe());
        fragmentList.add(new Fragment_Life());
        fragmentList.add(new Fragment_Beautiful());

        adapter = new MyHomePagerAdapter(getFragmentManager(), fragmentList);
        vp.setAdapter(adapter);

    }

    private void initTitles() {
        for (int i = 0; i < titles.length; i++) {
            tablayout.addTab(tablayout.newTab().setText(titles[i]));
        }

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
